package com.trm.role.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.common.util.jdbc.BaseDao;

public class RoleDao extends BaseDao {

    private int page = 1;
    private int pageSize = 10;

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 获得所有角色信息
     * 
     * @return
     */
    public List<Map<String, Object>> getRoleList() {
        String sql = "select * from role";
        List<Map<String, Object>> list = this.executeSqlToList(sql);
        return list;
    }

    /**
     * 分页查询角色信息
     * 
     * @return
     */
    public List<Map<String, Object>> getRolePageList() {
        String sql = "select * from role";
        sql += " limit " + pageSize * (page - 1) + "," + pageSize;
        List<Map<String, Object>> list = this.executeSqlToList(sql);
        return list;
    }

    /**
     * 根据角色获得用户数量
     * 
     * @param roleId
     * @return
     */
    public int getRoleCount() {
        String sql = "select count(*) as count from role";
        Map<String, Object> countMap = this.executeSqlToMap(sql);
        int count = Integer.valueOf(countMap.get("count").toString());
        return count;
    }

    /**
     * 获得权限角色信息
     * 
     * @return
     */
    public List<Map<String, Object>> getRightRoleList(String right) {
        right = "'" + right + "'";
        String sql = "select * from role where " + right + "like CONCAT('%',id,'%')";
        List<Map<String, Object>> list = this.executeSqlToList(sql);
        return list;
    }

    /**
     * 根据角色id查询角色信息
     * 
     * @param id
     * @return
     */
    public Map<String, Object> getRoleById(String id) {
        String sql = "select * from role where id = " + id;
        Map<String, Object> map = this.executeSqlToMap(sql);
        return map;
    }

    /**
     * 根据id修改角色信息
     * 
     * @param id
     * @return
     */
    public int updateRoleById(Map<String, String> params) {
        String id = params.get("id");
        if (id == null || "".equals(id)) {
            System.out.println("未获取到id");
            return -1;
        }
        String column = "";
        for (Map.Entry<String, String> entry : params.entrySet()) {
            column += ",`" + entry.getKey() + "` = '" + entry.getValue() + "'";
        }
        if ("".equals(column)) {
            System.out.println("请检查参数");
            return -1;
        }
        column = column.substring(1);
        String sql = "update role set " + column + " where id = " + id;
        int result = this.executeSql(sql);
        return result;
    }

    /**
     * 添加角色信息
     * 
     * @param list
     */
    public int addRole(Map<String, String> role) {
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        list.add(role);
        int result = this.insert(list, "role");
        return result;
    }

    /**
     * 根据id删除角色
     * 
     * @param id
     * @return
     */
    public int deleteRoleById(String id) {
        String sql = "delete from role where id = " + id;
        int result = this.executeSql(sql);
        return result;
    }

    /**
     * 取属性信息
     * 
     * @return
     */
    public List<Map<String, Object>> getProperties() {
        String sql = "select * from property where status <> -1";
        List<Map<String, Object>> list = this.executeSqlToList(sql);
        return list;
    }
}
